#include<stdio.h>

int isSeven(int num);
int main(void)
{
	int t,N,flag;
	scanf("%d",&t);
	for(int i=0;i<t;i++)
	{
		scanf("%d",&N);
		for(int i=1;i<=N;i++)
		{
			if(flag=isSeven(i))
			{
				printf("%d ",i);
			}
		}
		putchar('\n');
	}
	
	return 0;
}
int isSeven(int num)
{
	int wan=0,qian=0,bai=0,shi=0,ge=0;
	wan=num/10000%10;
	qian=num/1000%10;
	bai=num/100%10;
	shi=num/10%10;
	ge=num%10;
	
	if(wan==7||qian==7||bai==7||shi==7||ge==7||!(num%7))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
